var axios = require("axios");

var config = {
  method: "get",
  url: "https://zkcsmdj.zhoukouzhengwu.com/zkcsm/ApiInter/wechatCallback?code=071VpIFa1crYTD0QcPHa1ce8Ud3VpIFg&state=placeCode_P9d6cd51100a74fd6b3cac2500b58dff0-unitArea_411621203201-utoken_04688ecad6287b2be952bf5186affd72",
};

const $axios = axios.create({
  withCredentials: true,
  headers: {
    Cookie: "JSESSIONID=8AF7E052F69FEF87F54BCD6C4026CEC7; SF_cookie_12=18777903",
  },
});

const noop = v => v;

// 延时执行
function delay(timer, cb = noop) {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(cb());
    }, timer);
  });
}

function loop(cb, wait = 3000) {
  Promise.resolve()
    .then(() => {
      return cb;
    })
    .then(res => {
      if (res.data.includes("安全提示")) {
        console.log("扫码成功", `${++count}/${++total}`);
      } else {
        console.log("本次失效", `${count}/${++total}`);
      }
      return delay(wait);
    })
    .then(() => {
      loop(cb);
    });
}

let count = 0;
let total = 0;
loop($axios(config));
